package helpers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

public class LanguageHelper {
	
	private ResourceBundle translate;
	
	private String language = "en";
	
	private static List<String> languages = new ArrayList<String>(Arrays.asList("en", "es", "pt"));
	
	public static List<String> getLanguages() {
		return languages;
	}
	
	public LanguageHelper(HttpServletRequest request) {
		String url = UrlHelper.getServerUrl(request);
		language = getLanguage(url);
		translate = ResourceBundle.getBundle("language", new Locale(language));
	}
	
	public String getLanguage() {
		return language;
	}
	
	public String getLanguage(String url) {
		String firstSubDomain = UrlHelper.getFirstSubDomain(url);
		if (languages.contains(firstSubDomain)) {
			language = firstSubDomain;
		}
		
		return language;
	}
	
	public String getString(String key) {
		String value = "";
		try {
			value = translate.getString(key);
		} catch (Exception e) {}
		return value; 
	}
}
